package com.jlkf.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.SystemMapper;
import com.jlkf.service.BackCourseInfoService;
import com.jlkf.service.SystemService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("/system")
public class SystemController {
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private SystemMapper systemMapper;
	
	@Autowired
	private BackCourseInfoService backCourseInfoService;
	
	
	/**
	 * 职业方向列表
	 */
	@ResponseBody
	@RequestMapping("/selectJobDirectionList")
	public Object selectJobDirectionList(String startTime,String endTime,String G_TITLE,Integer id,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("G_TITLE", G_TITLE);
		map.put("G_ID", id);
		map.put("G_TYPE", 2);
		if(id != null){
			ModelAndView modelAndView = new ModelAndView();
			Integer[] featrue = systemMapper.selectJobCourse(id);
			String str = "";
			for(int i = 0;i < featrue.length;i++){
				str += featrue[i] + ",";
			}
			if(!StringTools.isEmpty(str)){
				str = str.substring(0, str.length() - 1);
			}
			modelAndView.setViewName("jsp/systemManager/updateJobDirection");
			List<Map<String, Object>> lists = systemService.selectJobDirectionList(map);
			lists.get(0).put("str",str);
			modelAndView.addObject("result", lists.get(0));
			List<Map<String, Object>> data = backCourseInfoService.selectCourseInfoList(map);
			modelAndView.addObject("data", data);
			return modelAndView;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<Map<String, Object>> lists = systemService.selectJobDirectionList(map);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"职业方向列表");
	}
	
	/**
	 * 删除职业方向
	 */
	@ResponseBody
	@RequestMapping("/deleteJobDirection")
	public Object deleteJobDirection(String id){
		int flag = systemService.deleteJobDirection(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 添加职业方向
	 */
	@ResponseBody
	@RequestMapping("/insertJobDirection")
	public Object insertJobDirection(String app_img,String G_TITLE,String G_CONTENT,String G_IMG,String job_image,Integer discount,String courseid,
			String pc_cover,String app_cover){
		Map<String,Object> map = new HashMap<>();
		map.put("app_img", app_img);
		map.put("G_TITLE", G_TITLE);
		map.put("G_CONTENT", G_CONTENT);
		map.put("G_IMG", G_IMG);
		map.put("job_image", job_image);
		map.put("discount", discount);
		map.put("pc_cover", pc_cover);
		map.put("app_cover", app_cover);
		int flag =  systemService.insertJobDirection(map);
		Long G_COURSE_SUPER_TYPE_ID = (Long) map.get("G_ID");
		String[] split = courseid.split(",");
		for(int i = 0;i < split.length;i++){
			//更新课程信息
			//systemMapper.updateCourseInfo(Integer.parseInt(split[i]), 2, G_COURSE_SUPER_TYPE_ID.intValue());
			systemMapper.updateCourseIsJobDirection(Integer.parseInt(split[i]),G_COURSE_SUPER_TYPE_ID.intValue());
			
		}
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"添加失败");
		}
	}
	
	/**
	 * 编辑职业方向
	 */
	@ResponseBody
	@RequestMapping("/updateJobDirection")
	public Object updateJobDirection(String app_img,Integer G_ID,String job_image,String G_TITLE,String G_CONTENT,String G_IMG,Integer discount,String courseid,
			String pc_cover,String app_cover){
		Map<String,Object> map = new HashMap<>();
		map.put("G_ID", G_ID);
		map.put("app_img", app_img);
		map.put("job_image", job_image);
		map.put("G_CONTENT", G_CONTENT);
		map.put("G_TITLE", G_TITLE);
		map.put("G_IMG", G_IMG);
		map.put("discount", discount);
		map.put("pc_cover", pc_cover);
		map.put("app_cover", app_cover);
		if(!StringTools.isEmpty(courseid)){
			String[] split = courseid.split(",");
			for(int i = 0;i < split.length;i++){
				//更新课程信息
				//systemMapper.updateCourseInfo(Integer.parseInt(split[i]), 2, G_ID);
				systemMapper.updateCourseIsJobDirection(Integer.parseInt(split[i]),G_ID);
			}
			//Long[] selectCourseId = systemMapper.selectCourseId(G_ID,2);
			Long[] selectCourseId = systemMapper.selectJobCourseId(G_ID);
			for(int i = 0;i < selectCourseId.length;i++){
				if(!courseid.contains(selectCourseId[i].toString())){
					//systemMapper.updatejobCourse(selectCourseId[i].intValue());
					systemMapper.updateJobCourseIsNot(selectCourseId[i].intValue());
				}
			}
		}
		int flag =  systemService.updateJobDirection(map);
		if(flag > 0){
			return new ReturnData(200,"修改成功");
		}else{
			return new ReturnData(500,"修改失败");
		}
	}
	
	/**
	 * 知识体系列表
	 */
	@ResponseBody
	@RequestMapping("/selectknowledgeList")
	public Object selectknowledgeList(String startTime,String endTime,String G_TITLE,Integer id,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("G_TITLE", G_TITLE);
		map.put("G_ID", id);
		map.put("G_TYPE", 3);
		if(id != null){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("jsp/systemManager/updateKnowledge");
			List<Map<String, Object>> lists = systemService.selectknowledgeList(map);
			modelAndView.addObject("result", lists.get(0));
			return modelAndView;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<Map<String, Object>> lists = systemService.selectknowledgeList(map);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"知识体系列表");
	}
	
	/**
	 * 添加知识体系
	 */
	@ResponseBody
	@RequestMapping("/insertKnowledge")
	public Object insertKnowledge(String app_img,String G_TITLE,String G_CONTENT,String G_IMG,Integer discount,String pc_cover,String app_cover){
		Map<String,Object> map = new HashMap<>();
		map.put("app_img", app_img);
		map.put("G_TITLE", G_TITLE);
		map.put("G_CONTENT", G_CONTENT);
		map.put("pc_cover", pc_cover);
		map.put("app_cover", app_cover);
		map.put("G_IMG", G_IMG);
		map.put("discount", discount);
		int flag =  systemService.insertKnowledge(map);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"添加失败");
		}
	}
	
	/**
	 * 阶段列表
	 */
	@ResponseBody
	@RequestMapping("/stageList")
	public Object stageList(Integer id,Integer stageid,Integer pageNumber,Integer pageSize){
		if(stageid != null){
			ModelAndView modelAndView = new ModelAndView();
			String str = systemMapper.selectKnowledgeCourse(stageid);
			modelAndView.setViewName("jsp/systemManager/updateStage");
			Map<String, Object> lists = systemMapper.selectStageInfo(stageid);
			lists.put("str",str);
			modelAndView.addObject("result", lists);
			List<Map<String, Object>> data = backCourseInfoService.selectCourseInfoList(new HashMap<>());
			modelAndView.addObject("data", data);
			return modelAndView;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<Map<String, Object>> lists = systemService.stageList(id);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"阶段列表");
	}
	
	/**
	 * 查看课程列表
	 */
	@ResponseBody
	@RequestMapping("/courseList")
	public Object selectLessonList(Integer id,Integer pageNumber,Integer pageSize){
		String selectKnowledgeCourse = systemMapper.selectKnowledgeCourse(id);
		String[] split = selectKnowledgeCourse.split(",");
		PageHelper.startPage(pageNumber, pageSize);
		List<Map<String,Object>> lists = new LinkedList<>();
		for(int i = 0;i < split.length;i++){
			Map<String, Object> map = systemMapper.courseList(Integer.parseInt(split[i]));
			if(map != null){
				lists.add(map);
			}
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"课程列表");
	}
	
	/**
	 * 课程置顶
	 */
	@ResponseBody
	@RequestMapping("/courseStick")
	public Object courseStick(Integer id,String courseid){
		Map<String, Object> map = systemMapper.selectStageInfo(id);
		String courses = map.get("course_id").toString();
		if(courses.contains(courseid + ",")){
			courses = courses.replace(courseid + ",", "");
			courses = courseid + "," + courses;
		}
		if(courses.contains("," + courseid)){
			courses = courses.replace("," + courseid, "");
			courses = courseid + "," + courses;
		}
		Map<String,Object> hashMap = new HashMap<>();
		hashMap.put("id", id);
		hashMap.put("course_id", courses);
		int flag = systemMapper.updateStage(hashMap);
		if(flag > 0){
			return new ReturnData(200,"置顶成功");
		}else{
			return new ReturnData(500,"置顶失败");
		}
	}
	
	/**
	 * 添加阶段
	 */
	@ResponseBody
	@RequestMapping("/insertStage")
	public Object insertStage(Integer catalogue,String title,String content,Integer profession_sort_id,String courseid){
		Map<String,Object> hashMap = new HashMap<>();
		hashMap.put("catalogue", catalogue);
		hashMap.put("profession_sort_id", profession_sort_id);
		hashMap.put("title", title);
		hashMap.put("content", content);
		hashMap.put("courseid", courseid);
		int flag = systemMapper.insertStage(hashMap);
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"添加失败");
		}
	}
	
	/**
	 * 删除阶段
	 */
	@ResponseBody
	@RequestMapping("/deleteStage")
	public Object deleteStage(String id){
		int flag = systemService.deleteStage(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 编辑阶段
	 */
	@ResponseBody
	@RequestMapping("/updateStage")
	public Object updateStage(Integer id,Integer catalogue,String title,String content,String courseid){
		Map<String,Object> map = new HashMap<>();
		map.put("id", id);
		map.put("catalogue", catalogue);
		map.put("title", title);
		map.put("content", content);
		map.put("course_id", courseid);
		int flag =  systemService.updateStage(map);
		if(flag > 0){
			return new ReturnData(200,"修改成功");
		}else{
			return new ReturnData(500,"修改失败");
		}
	}
	
	/**
	 * 特色课程列表
	 */
	@ResponseBody
	@RequestMapping("/selectFeatureList")
	public Object selectFeatureList(String startTime,String endTime,String G_TITLE,Integer id,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("G_TITLE", G_TITLE);
		map.put("G_ID", id);
		map.put("G_TYPE", 4);
		if(id != null){
			ModelAndView modelAndView = new ModelAndView();
			Integer[] featrue = systemMapper.selectFeatrueCourse(id);
			String str = "";
			for(int i = 0;i < featrue.length;i++){
				str += featrue[i] + ",";
			}
			str = str.substring(0, str.length() - 1);
			modelAndView.setViewName("jsp/systemManager/updateFeatrue");
			List<Map<String, Object>> lists = systemService.selectJobDirectionList(map);
			lists.get(0).put("str",str);
			modelAndView.addObject("result", lists.get(0));
			List<Map<String, Object>> data = backCourseInfoService.selectCourseInfoList(map);
			modelAndView.addObject("data", data);
			return modelAndView;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<Map<String, Object>> lists = systemService.selectJobDirectionList(map);
		for(int i = 0;i < lists.size();i++){
			Integer feature_one_stair_id = (Integer) lists.get(i).get("feature_one_stair_id");
			String oneName = systemMapper.selectFeatrueName(feature_one_stair_id);
			Integer feature_two_stair_id = (Integer) lists.get(i).get("feature_two_stair_id");
			String twoName = systemMapper.selectFeatrueName(feature_two_stair_id);
			Integer feature_three_stair_id = (Integer) lists.get(i).get("feature_three_stair_id");
			String threeName = systemMapper.selectFeatrueName(feature_three_stair_id);
			lists.get(i).put("oneName", oneName);
			lists.get(i).put("twoName", twoName);
			lists.get(i).put("threeName", threeName);
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"特色课程列表");
	}
	
	/**
	 * 查看打包课程里面的课程列表
	 */
	@ResponseBody
	@RequestMapping("/selectFeatureCourseList")
	public Object selectFeatureCourseList(Integer id,Integer pageNumber,Integer pageSize){
		String featrue = systemMapper.selectFeatureCourseid(id);
		String[] split = featrue.split(",");
		//PageHelper.startPage(pageNumber, pageSize);
		List<Map<String,Object>> lists = new LinkedList<>();
		for(int i = 0;i < split.length;i++){
			Map<String, Object> map = systemMapper.selectFeatureCourseList(split[i]);
			lists.add(map);
		}
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("str", featrue);
		}
		//PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
		return new ReturnData(lists,/*pageInfo.getPages(),(int)pageInfo.getTotal(),*/200,"特色课程列表");
	}
	
	/**
	 * 特色课程置顶
	 */
	@ResponseBody
	@RequestMapping("/featureCourseStick")
	public Object featureCourseStick(Integer id,String courseid){
		String courses = systemMapper.selectFeatureCourseid(id);
		if(courses.contains(courseid + ",")){
			courses = courses.replace(courseid + ",", "");
			courses = courseid + "," + courses;
		}
		if(courses.contains("," + courseid)){
			courses = courses.replace("," + courseid, "");
			courses = courseid + "," + courses;
		}
		Map<String,Object> hashMap = new HashMap<>();
		hashMap.put("G_ID", id);
		hashMap.put("courseid", courses);
		int flag = systemMapper.updateJobDirection(hashMap);
		if(flag > 0){
			return new ReturnData(200,"置顶成功");
		}else{
			return new ReturnData(500,"置顶失败");
		}
	}
	
	/**
	 * 添加特色课程
	 */
	@ResponseBody
	@RequestMapping("/insertFeatrue")
	public Object insertFeatrue(String app_img,String G_TITLE,String G_CONTENT,String G_IMG,String content,Integer discount,String courseid,Integer feature_one_stair_id,
			Integer feature_two_stair_id,Integer feature_three_stair_id,String school,String end_time,String start_time,String pc_cover,String app_cover){
		Map<String,Object> map = new HashMap<>();
		map.put("app_img", app_img);
		map.put("G_TITLE", G_TITLE);
		map.put("G_CONTENT", G_CONTENT);
		map.put("G_IMG", G_IMG);
		map.put("pc_cover", pc_cover);
		map.put("app_cover", app_cover);
		map.put("school", school);
		map.put("end_time", end_time);
		map.put("start_time", start_time);
		map.put("content", content);
		map.put("discount", discount);
		map.put("courseid", courseid);
		int flag =  systemService.insertFeatrue(map);
		Long G_COURSE_SUPER_TYPE_ID = (Long) map.get("G_ID");
		String[] split = courseid.split(",");
		for(int i = 0;i < split.length;i++){
			//更新课程信息
			systemMapper.updateCourse(Integer.parseInt(split[i]), G_COURSE_SUPER_TYPE_ID.intValue(),feature_one_stair_id,feature_two_stair_id,feature_three_stair_id);
		}
		if(flag > 0){
			return new ReturnData(200,"添加成功");
		}else{
			return new ReturnData(500,"添加失败");
		}
	}
	
	/**
	 * 查看特色课程一级二级跟三级分类
	 */
	@ResponseBody
	@RequestMapping("/selectFeatrueCourseOneStair")
	public Object selectFeatrueCourseOneStair(Integer sid){
		List<Map<String,Object>> lists = systemService.selectFeatrueCourseOneStair(sid);
		return new ReturnData(lists,200,"分类列表");
	}
	
	/**
	 * 编辑特色课程
	 */
	@ResponseBody
	@RequestMapping("/updateFeatrue")
	public Object updateFeatrue(String app_img,Integer G_ID,String content,String G_TITLE,String G_CONTENT,String G_IMG,Integer discount,String courseid,
			Integer feature_one_stair_id,Integer feature_two_stair_id,Integer feature_three_stair_id,String school,String end_time,String start_time,String pc_cover,String app_cover){
		Map<String,Object> map = new HashMap<>();
		map.put("G_ID", G_ID);
		map.put("app_img", app_img);
		map.put("pc_cover", pc_cover);
		map.put("app_cover", app_cover);
		map.put("content", content);
		map.put("school", school);
		map.put("end_time", end_time);
		map.put("start_time", start_time);
		map.put("G_CONTENT", G_CONTENT);
		map.put("G_TITLE", G_TITLE);
		map.put("G_IMG", G_IMG);
		map.put("courseid", courseid);
		map.put("discount", discount);
		if(!StringTools.isEmpty(courseid)){
			String[] split = courseid.split(",");
			for(int i = 0;i < split.length;i++){
				//更新课程信息
				systemMapper.updateCourse(Integer.parseInt(split[i]), G_ID,feature_one_stair_id,feature_two_stair_id,feature_three_stair_id);
			}
			Long[] selectCourseId = systemMapper.selectCourseId(G_ID,4);
			for(int i = 0;i < selectCourseId.length;i++){
				if(!courseid.contains(selectCourseId[i].toString())){
					systemMapper.updateFeatrueCourse(selectCourseId[i].intValue());
				}
			}
		}
		int flag =  systemService.updateJobDirection(map);
		if(flag > 0){
			return new ReturnData(200,"修改成功");
		}else{
			return new ReturnData(500,"修改失败");
		}
	}
	
}
